home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 121 < prev    next >
Internet Message Format  |  1996-08-06  |  2KB

  1. Path: teal.csn.net!not-for-mail
  2. From: thads@csn.net (Thad Smith)
  3. Newsgroups: comp.std.c
  4. Subject: Re: Help, best way to compare doubles
  5. Date: 17 Jan 1996 01:51:23 -0700
  6. Organization: T3 Systems
  7. Message-ID: <mlK/wQ9ytpDN084yn@csn.net>
  8. References: <4d1k09$aqq@mercury.IntNet.net> <4dbos6$o7q@umbc9.umbc.edu>
  9.  <4ddev4$1vg@stingray.mcnc.org>
  10. Reply-To: ThadSmith@acm.org
  11. NNTP-Posting-Host: 199.117.27.22
  12.  
  13. In article <4ddev4$1vg@stingray.mcnc.org>,
  14. coats@mcnc.org (Carlie Coats) wrote:
  15.  
  16. >>14.5:    What's a good way to check for "close enough" floating-point
  17. >>    equality?
  18.  
  19. > A safer test is of the form
  20. >
  21. >    | a - b | / sqrt( a^2 + b^2 + delta ) < epsilon
  22.  
  23. This criteria, plus the squared version which uses squares, seems
  24. to be too much effort with no payback.
  25.  
  26. Considering that the point of evaluating the approximate magnitude of
  27. the numbers being compared is only to make a judgement of whether two
  28. numbers are approximately the same, and that the decision point is
  29. only close for two numbers that are approximately the same, evaluating
  30. a square root or sum or squares buys nothing.  How about
  31. (abs(a)+abs(b))/2?  The factor 2 can be absorbed in the epsilon, just
  32. as well as the similar factor sqrt(2) that applies to the quoted
  33. expression. 
  34.  
  35. >or (equvalently, but in decently-efficient C):
  36. >
  37. >    ( t = a - b )*t < esquared * ( a*a + b*b  + delta )
  38. >
  39. >The choice of esquared == delta == 1e-20  gives this a
  40. >colloquial English meaning of 
  41. >
  42. >    "a and b agree to about 10 significant digits"
  43. >
  44. >which is a reasonable version of "are approximately equal"
  45. >for doubles.
  46.  
  47. Besides the undefined nature pointed out by Tanmoy, we need to be
  48. careful here in equating esquared and delta because they
  49. perform different functions.  While esquared is closely related to the
  50. number of matching significant digits, delta allows the relative
  51. difference to become larger as the magnitude of numbers get smaller.
  52. Using the values above, when the magnitude of the two numbers are less
  53. than 1e-20 and of the same sign, then the relative difference may be
  54. ANYTHING and the near-equality test will pass.  This will probably be
  55. overly restrictive or insufficiently restrictive, depending on the
  56. general magnitudes of the numbers involved.
  57.  
  58. The biggest problem, I think, in using such a criteria is in
  59. understanding the proper limits to be applied.  If we are working with
  60. 3-digit data, a 10-digit derived value is almost certainly overkill.
  61.  
  62. Thad
  63.  
  64.